/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.modules.system.service.impl;

import me.zhengjie.modules.system.domain.PurchaseRequestDetail;
import me.zhengjie.exception.EntityExistException;
import me.zhengjie.utils.ValidationUtil;
import me.zhengjie.utils.FileUtil;
import lombok.RequiredArgsConstructor;
import me.zhengjie.modules.system.repository.PurchaseRequestDetailRepository;
import me.zhengjie.modules.system.service.PurchaseRequestDetailService;
import me.zhengjie.modules.system.service.dto.PurchaseRequestDetailDto;
import me.zhengjie.modules.system.service.dto.PurchaseRequestDetailQueryCriteria;
import me.zhengjie.modules.system.service.mapstruct.PurchaseRequestDetailMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import me.zhengjie.utils.PageUtil;
import me.zhengjie.utils.QueryHelp;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import me.zhengjie.utils.PageResult;

/**
* @website https://eladmin.vip
* @description 服务实现
* @author 李忠臻
* @date 2024-02-20
**/
@Service
@RequiredArgsConstructor
public class PurchaseRequestDetailServiceImpl implements PurchaseRequestDetailService {

    private final PurchaseRequestDetailRepository purchaseRequestDetailRepository;
    private final PurchaseRequestDetailMapper purchaseRequestDetailMapper;

    @Override
    public PageResult<PurchaseRequestDetailDto> queryAll(PurchaseRequestDetailQueryCriteria criteria, Pageable pageable){
        Page<PurchaseRequestDetail> page = purchaseRequestDetailRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(purchaseRequestDetailMapper::toDto));
    }

    @Override
    public List<PurchaseRequestDetailDto> queryAll(PurchaseRequestDetailQueryCriteria criteria){
        return purchaseRequestDetailMapper.toDto(purchaseRequestDetailRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    @Transactional
    public PurchaseRequestDetailDto findById(Long id) {
        PurchaseRequestDetail purchaseRequestDetail = purchaseRequestDetailRepository.findById(id).orElseGet(PurchaseRequestDetail::new);
        ValidationUtil.isNull(purchaseRequestDetail.getId(),"PurchaseRequestDetail","id",id);
        return purchaseRequestDetailMapper.toDto(purchaseRequestDetail);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void create(PurchaseRequestDetail resources) {
        if(purchaseRequestDetailRepository.findByIndex(resources.getIndex()) != null){
            throw new EntityExistException(PurchaseRequestDetail.class,"index",resources.getIndex().toString());
        }
        purchaseRequestDetailRepository.save(resources);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(PurchaseRequestDetail resources) {
        PurchaseRequestDetail purchaseRequestDetail = purchaseRequestDetailRepository.findById(resources.getId()).orElseGet(PurchaseRequestDetail::new);
        ValidationUtil.isNull( purchaseRequestDetail.getId(),"PurchaseRequestDetail","id",resources.getId());
        PurchaseRequestDetail purchaseRequestDetail1 = null;
        purchaseRequestDetail1 = purchaseRequestDetailRepository.findByIndex(resources.getIndex());
        if(purchaseRequestDetail1 != null && !purchaseRequestDetail1.getId().equals(purchaseRequestDetail.getId())){
            throw new EntityExistException(PurchaseRequestDetail.class,"index",resources.getIndex().toString());
        }
        purchaseRequestDetail.copy(resources);
        purchaseRequestDetailRepository.save(purchaseRequestDetail);
    }

    @Override
    public void deleteAll(Long[] ids) {
        for (Long id : ids) {
            purchaseRequestDetailRepository.deleteById(id);
        }
    }

    @Override
    public void download(List<PurchaseRequestDetailDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (PurchaseRequestDetailDto purchaseRequestDetail : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("序号", purchaseRequestDetail.getIndex());
//            map.put("b2b号", purchaseRequestDetail.getB2b());
            map.put("型号", purchaseRequestDetail.getModel());
            map.put("数量", purchaseRequestDetail.getQuantity());
//            map.put("期望交付日", purchaseRequestDetail.getExpectedDeliveryDate());
            map.put("备注", purchaseRequestDetail.getRemark());
            map.put("客户", purchaseRequestDetail.getCustomer());
//            map.put("创建者", purchaseRequestDetail.getCreateBy());
//            map.put("更新者", purchaseRequestDetail.getUpdateBy());
//            map.put("创建时间", purchaseRequestDetail.getCreateTime());
//            map.put("更新时间", purchaseRequestDetail.getUpdateTime());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }

    @Override
    public List queryByPurchaseRequestId(Long purchaseRequestId) {
        return purchaseRequestDetailRepository.findByPurchaseRequestId(purchaseRequestId);
    }
}